myApp.filter('prodTFilter', [function () {
    return function (alist, isOnlyExport, isOnlyClean, isOnlyConfirm) {
        var list = [];

        angular.forEach(alist, function (t) {
            if (isOnlyExport && t.export_script != '') {
                return
            }
            if (isOnlyClean && !(t.is_need_clean == 'Y' && t.clean_script == '')) {
                return
            }
            if (isOnlyConfirm && !(t.is_check_rows == 'Y' && (t.check_rows == '' || t.check_rows == '0'))) {
                return
            }
            list.push(t);
        });
        return list;
    };
}]);
myApp.controller('ProdExportController', function ($scope, $stateParams, ngDialog, $http, $uibModal, $interval, $filter) {
    $scope.info = {};
    $scope.info.schemaList = [];
    $scope.info.schemaInfo = {};
    $scope.info.isOnlyExport = false;
    $scope.info.isOnlyClean = false;
    $scope.info.isOnlyConfirm = false;

    $scope.info.tableAllList = [];
    $scope.info.tableShowList = [];

    //查询库列表
    $scope.getSchemaList = function () {
        doBeetleGet($http, $uibModal, beetle_plan_url + '/prod/schema', function (data) {
            if (data.success) {
                $scope.info.schemaList = data.data;
                $scope.info.schemaInfo = null;
                if ($scope.info.schemaList.length > 0) {
                    for (var i in $scope.info.schemaList) {
                        var s = $scope.info.schemaList[i];
                        if (s.table_schema == 'cloudlink_epc') {
                            $scope.info.schemaInfo = s;
                            break;
                        }
                    }
                    if ($scope.info.schemaInfo == null) {
                        $scope.info.schemaInfo = $scope.info.schemaList[0];
                    }
                    $scope.getTableList();
                }
            } else {
                openDialogAlert(ngDialog, $scope, data.msg);
            }
        })
    };

    $scope.getSchemaList();

    $scope.getTableList = function () {
        doBeetleGet($http, $uibModal, beetle_plan_url + '/prod/list?schemaName=' + $scope.info.schemaInfo.table_schema, function (data) {
            if (data.success) {
                $scope.info.tableAllList = data.data;
                $scope.info.tableShowList = data.data;
                $scope.tableData();
            } else {
                openDialogAlert(ngDialog, $scope, data.msg);
            }
        })
    };

    $scope.schemaSelect = function () {
        $scope.getTableList();
    };

    $scope.tableData = function () {
        $scope.info.tableShowList = $filter('prodTFilter')($scope.info.tableAllList, $scope.info.isOnlyExport, $scope.info.isOnlyClean, $scope.info.isOnlyConfirm);
    };

    $scope.onExport = function (info) {
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'ProdExport.html',
            controller: 'ProdExportCtrl',
            resolve: {
                info: function () {
                    return info;
                }
            }
        });
        modalInstance.result.then(function (result) {
                if (result == 'save' || getAndRemoveStorage("table-flush")) {
                    $scope.getTableList();
                }
            },
            function (reason) {
                if (reason == 'save' || getAndRemoveStorage("table-flush")) {
                    $scope.getTableList();
                }
            });
    };
    $scope.onClean = function (info) {
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'ProdClean.html',
            controller: 'ProdCleanCtrl',
            resolve: {
                info: function () {
                    return info;
                }
            }
        });
        modalInstance.result.then(function (result) {
                if (result == 'save' || getAndRemoveStorage("table-flush")) {
                    $scope.getTableList();
                }
            },
            function (reason) {
                if (reason == 'save' || getAndRemoveStorage("table-flush")) {
                    $scope.getTableList();
                }
            });
    };
    $scope.onCheckRow = function (info) {
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'ProdCheckRow.html',
            controller: 'ProdCheckRowCtrl',
            resolve: {
                info: function () {
                    return info;
                }
            }
        });
        modalInstance.result.then(function (result) {
                if (result == 'save' || getAndRemoveStorage("table-flush")) {
                    $scope.getTableList();
                }
            },
            function (reason) {
                if (reason == 'save' || getAndRemoveStorage("table-flush")) {
                    $scope.getTableList();
                }
            });
    };
});
myApp.controller('ProdExportCtrl', function ($scope, $uibModalInstance, ngDialog, $uibModal, $http, info) {
    $scope.info = {};
    $scope.info.exportInfo = {};

    $http.get(beetle_plan_url + '/prod/export?schemaName=' + info.table_schema + '&tableName=' + info.table_name).success(function (data) {
        if (data.success) {
            if (data.data.length > 0) {
                $scope.info.exportInfo = data.data[0];
            } else {
                $scope.info.exportInfo.table_schema = info.table_schema;
                $scope.info.exportInfo.table_name = info.table_name;
                $scope.info.exportInfo.env_type = 'DEVTEST';
            }
            $scope.info.exportInfo.manager = info.manager;
        } else {
            openDialogAlert(ngDialog, $scope, data.msg);
        }
    });

    $scope.save = function () {
        var data = {};
        angular.copy($scope.info.exportInfo, data);
        data.updatedby = getUserAccount();
        console.log(data);

        openDialog(ngDialog, $scope, "确定要保存修改吗？", true, function (val) {
            if (!val) {
                return;
            }
            doBeetlePost($http, $uibModal, beetle_plan_url + '/prod/export', data, function (data) {
                if (data.success) {
                    $uibModalInstance.dismiss('save');
                } else {
                    openDialogAlert(ngDialog, $scope, data.msg);
                }
            });
        })
    };
    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
});

myApp.controller('ProdCleanCtrl', function ($scope, $uibModalInstance, ngDialog, $uibModal, $http, info) {
    $scope.info = {};
    $scope.info.baseInfo = info;
    $scope.info.cleanList = [];
    $scope.info.tableInfo = {};
    $scope.info.isNeedClean = false;

    $http.get(beetle_plan_url + '/prod/clean?schemaName=' + info.table_schema + '&tableName=' + info.table_name).success(function (data) {
        if (data.success) {
            $scope.info.cleanList = data.data;
        } else {
            openDialogAlert(ngDialog, $scope, data.msg);
        }
    });

    $http.get(beetle_plan_url + '/prod/table?schemaName=' + info.table_schema + '&tableName=' + info.table_name).success(function (data) {
        if (data.success) {
            $scope.info.tableInfo = data.data[0];
            if ($scope.info.tableInfo.is_need_clean == 'N') {
                $scope.info.isNeedClean = true;
            }
        } else {
            openDialogAlert(ngDialog, $scope, data.msg);
        }
    });

    $scope.add = function () {
        var obj = {};
        obj.id = null;
        obj.is_active = 'Y';
        obj.createdby = getUserAccount();
        $scope.info.cleanList.push(obj);
    };
    $scope.changeActive = function (info, active) {
        openDialog(ngDialog, $scope, "确定要设置成" + active == 'N' ? "无效" : "有效" + "吗？", true, function (val) {
            if (!val) {
                return;
            }
            info.is_active = active;
        });
    };

    $scope.updateTableClean = function () {
        openDialog(ngDialog, $scope, "确定要修改吗？", true, function (val) {
            var check = $scope.info.tableInfo.is_need_clean;
            if (!val) {
                if (check == 'N') {
                    $scope.info.isNeedClean = true;
                } else {
                    $scope.info.isNeedClean = false;
                }
                return;
            }
            if ($scope.info.isNeedClean) {
                check = 'N';
            } else {
                check = 'Y';
            }
            setStorage("table-flush", true);
            $http.get(beetle_plan_url + '/prod/table/checkrow?schemaName=' + info.table_schema + '&tableName=' + info.table_name + '&isNeedClean=' + check).success(function (data) {
            });
        });
    };

    $scope.save = function (i) {

        openDialog(ngDialog, $scope, "确定要保存修改吗？", true, function (val) {
            if (!val) {
                return;
            }
            var data = {};
            data.schemaName = $scope.info.baseInfo.table_schema;
            data.tableName = $scope.info.baseInfo.table_name;
            var list = [];
            i.updatedby = getUserAccount();
            list.push(i);
            data.list = JSON.stringify(list);
            console.log(data);
            doBeetlePost($http, $uibModal, beetle_plan_url + '/prod/clean', data, function (data) {
                if (data.success) {
                    $uibModalInstance.dismiss('save');
                } else {
                    openDialogAlert(ngDialog, $scope, data.msg);
                }
            });
        })
    };
    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
});
myApp.controller('ProdCheckRowCtrl', function ($scope, $uibModalInstance, ngDialog, $uibModal, $http, info) {
    $scope.info = {};
    $scope.info.checkInfo = {};
    $scope.info.tableInfo = {};
    $scope.info.isCheckRows = false;

    $http.get(beetle_plan_url + '/prod/checkRow?schemaName=' + info.table_schema + '&tableName=' + info.table_name).success(function (data) {
        if (data.success) {
            $scope.info.checkInfo = data.data;
            $scope.info.checkInfo.manager = info.manager;
        } else {
            // openDialogAlert(ngDialog, $scope, data.msg);
            $scope.info.checkInfo.manager = info.manager;
            $scope.info.checkInfo.this_rows = "0";
            $scope.info.checkInfo.checked_rows = "0";
            $scope.info.checkInfo.checked_date = new Date();
            $scope.info.checkInfo.checkedby = getUserAccount();
        }
    });

    $http.get(beetle_plan_url + '/prod/table?schemaName=' + info.table_schema + '&tableName=' + info.table_name).success(function (data) {
        if (data.success) {
            $scope.info.tableInfo = data.data[0];
            if (!$scope.info.checkInfo.table_schema) {
                $scope.info.checkInfo.table_schema = $scope.info.tableInfo.table_schema;
                $scope.info.checkInfo.table_name = $scope.info.tableInfo.table_name;
            }
            if ($scope.info.tableInfo.is_check_rows == 'N') {
                $scope.info.isCheckRows = true;
            }
        } else {
            openDialogAlert(ngDialog, $scope, data.msg);
        }
    });

    $scope.updateTableCheck = function () {
        openDialog(ngDialog, $scope, "确定要修改吗？", true, function (val) {
            var check = $scope.info.tableInfo.is_check_rows;
            if (!val) {
                if (check == 'N') {
                    $scope.info.isCheckRows = true;
                } else {
                    $scope.info.isCheckRows = false;
                }
                return;
            }

            if ($scope.info.isCheckRows) {
                check = 'N';
            } else {
                check = 'Y';
            }
            setStorage("table-flush", true);
            $http.get(beetle_plan_url + '/prod/table/checkrow?schemaName=' + info.table_schema + '&tableName=' + info.table_name + '&isCheckRows=' + check).success(function (data) {
            });
        });
    };

    $scope.save = function () {

        openDialog(ngDialog, $scope, "确定要保存修改吗？", true, function (val) {
            if (!val) {
                return;
            }
            var data = {};
            angular.copy($scope.info.checkInfo, data);
            data.checkedby = getUserAccount();
            console.log(data);
            doBeetlePost($http, $uibModal, beetle_plan_url + '/prod/checkRow', data, function (data) {
                if (data.success) {
                    $uibModalInstance.dismiss('save');
                } else {
                    openDialogAlert(ngDialog, $scope, data.msg);
                }
            });
        })
    };
    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
});